package report

import (
	"fmt"
	"go-interview-forum/model/report"
)

func GetCateMasterQuantity(userId int) ([]report.Result, error) {
	//获取分类掌握数量
	data, err := report.GetCateQuantity(userId)
	if err != nil {
		return nil, err
	}
	return data, err
}

// GetCateSumQuantity 获取分类掌握总数量
func GetCateSumQuantity() ([]report.Res, error) {
	//获取分类掌握总数量
	data, err := report.GetCateSumQuantity()
	if err != nil {
		return nil, err
	}
	return data, err
}

// MasteryRates 定义一个结构体来存储分类下的掌握率和未掌握率
type MasteryRates struct {
	CategoryName    string
	MasteryRate     string
	UnfulfilledRate string
}

func GetUserMasteryRates(userId int) ([]MasteryRates, error) {
	// 获取每个分类下题目的总数
	cateSumQuantity, err := report.GetCateSumQuantity()
	if err != nil {
		return nil, err
	}
	fmt.Println("Category Total Quantity:", cateSumQuantity)

	// 获取每个分类下掌握题目的数量
	cateQuantity, err := report.GetCateQuantity(userId)
	if err != nil {
		return nil, err
	}
	fmt.Println("Category Mastered Quantity:", cateQuantity)

	// 创建一个切片用于存储每个分类的掌握率和未掌握率
	var masteryRates []MasteryRates

	// 遍历每个分类，计算掌握率和未掌握率
	for category, sum := range cateSumQuantity {
		// 获取当前分类下的掌握题目数
		mastered := cateQuantity[category]

		// 计算掌握率和未掌握率
		var masteryRate, unfulfilledRate string
		if sum.TotalQuestions > 0 {
			mastery := float64(mastered.GraspedCount) / float64(sum.TotalQuestions) * 100.0
			unfulfilled := float64(sum.TotalQuestions-mastered.GraspedCount) / float64(sum.TotalQuestions) * 100.0

			masteryRate = fmt.Sprintf("%.2f%%", mastery)
			unfulfilledRate = fmt.Sprintf("%.2f%%", unfulfilled)
		} else {
			// 如果总题目数为0，设置为0%
			masteryRate = "0.00%"
			unfulfilledRate = "0.00%"
		}

		// 将掌握率和未掌握率添加到切片中
		masteryRates = append(masteryRates, MasteryRates{
			CategoryName:    sum.CategoryName,
			MasteryRate:     masteryRate,
			UnfulfilledRate: unfulfilledRate,
		})
	}

	return masteryRates, nil
}
